package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageResult pageSelectCategory(CategoryPageQueryDTO categoryPageQueryDTO) {

        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        Category category = Category.builder().name(categoryPageQueryDTO.getName()).type(categoryPageQueryDTO.getType()).build();
        Page<Category> categories = categoryMapper.pageSelectCategory(category);

        return new PageResult(categories.getTotal(), categories.getResult());
    }


    @Override
    public List<Category> byTypeSelectCategory(Integer type) {

        return categoryMapper.byTypeSelectCategory(type);
    }

    @Override
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public void updateCategory(CategoryDTO categoryDTO) {

        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        categoryMapper.updateCategory(category);
        //redisTemplate.delete("user_"+category.getId());
    }

    @Override
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public void insertCategory(CategoryDTO categoryDTO) {

        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        categoryMapper.insertCategory(category);
        //redisTemplate.delete("user_"+category.getId());
    }

    @Override
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public void statusStartOrStop(Integer status, Long id) {

        categoryMapper.updateCategory(Category.builder().status(status).id(id).build());
        //redisTemplate.delete("user_"+id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @CacheEvict(cacheNames = "userCache",allEntries = true)
    public void deleteCategoryById(Long id) {

        List<Dish> dishes = dishMapper.selectDishByCategoryId(id);
        if (!dishes.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        List<Setmeal> setmeals = setmealMapper.selectSetmealByCategoryId(id);
        if(!setmeals.isEmpty()){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        Category category = categoryMapper.selectCategoryById(id);
        if(category.getStatus()== StatusConstant.ENABLE){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_ON_SALE);
        }
        categoryMapper.deleteCategoryById(id);
/*        redisTemplate.delete("user_"+category.getId());*/

    }
}
